#!/bin/bash

basedir="/usr/local/lepus"

#check basedir
#############################################################################
if test -z "$basedir"
then
  basedir=/usr/local/lepus
else
  basedir="$basedir"
fi

arg=$1
lepus_process=`ps -ef|grep lepus.py |grep -v grep|wc -l`
case "$arg" in
  'status')
    #check lepus status
    if [ $lepus_process -ge "2" ];then
       echo "lepus server is running..."
    else
       echo "lepus server is not run!"
    fi
  ;;

  'start')
    #start lepus
    if [ $lepus_process -ge "2" ];then
       echo " lepus server is already run!"
    else
       cd $basedir && nohup  python lepus.py & > /dev/null 2>&1
       sleep 5
       lepus_process=`ps -ef|grep lepus.py |grep -v grep|wc -l`
       if [ $lepus_process -ge "2" ];then
          echo "lepus server start success!"
       else
          echo "lepus server start fail!"
       fi
    fi
  ;;

  'stop')
    #stop lepus
    if [ $lepus_process -le "2" ];then
       echo "lepus server is not running!"
    else
       ps -ef |grep -i -E "lepus.py"|grep -v -E "vi|grep"|awk '{print $2}' |while read line; do kill $line; echo "lepus processes id $line been stop"; done 
       #killall python lepus.py*
    fi
  ;;

  '--help')
    #for help
    echo "lepus help:"
    echo "support-site:  www.lepus.cc"
    echo "===================================================================="
    echo "start        Start lepus monitor server; Command: #lepus start"
    echo "stop         Stop lepus monitor server; Command: #lepus stop"
    echo "status       Check lepus monitor run status; Command: #lepus status"
  ;;
    *)
        echo "Please input  --help to read the help info."
    ;;

esac

exit 0  
  



